package com.infopulse;

import java.util.Arrays;

/**
 * Created by sky_lady on 5/18/14.
 */

public class HomeTask2 {

    public static int binarySearch(int[] array, int value, int low, int high) {
        if (low > high) return -1;
        int middle = (low + high) / 2;
        if (array[middle] == value)
            return middle;
        else if (array[middle] > value)
            return binarySearch(array, value, low, middle - 1);
        else
            return binarySearch(array, value, middle + 1, high);
    }

    public static int findMin(int array[], int low, int high) {
        if (high < low) return -1;
        if (high == low) return low;

        int middle = (low + high) / 2;
        if (middle < high && array[middle] > array[middle + 1])
            return middle;
        if (middle > low && array[middle] < array[middle - 1])
            return (middle - 1);
        if (array[low] >= array[middle])
            return findMin(array, low, middle - 1);
        else
            return findMin(array, middle + 1, high);
    }


    public static void main(String[] args) {
        int[] array = new int[]{2, 4, 5, 8, 9, 0, 6, 13, 7, 9, 12, 75, -12, -13};
        System.out.println("Array: " + Arrays.toString(array));

        //Arrays.sort(array);
        QuickSort.sort(array);
        System.out.println("Sorted Array: " + Arrays.toString(array));

        int m = findMin(array, 0, array.length - 1);
        System.out.println("Minimal element is " + array[0]);

        //System.out.println(binarySearch(array, 10, 0, array.length - 1));
    }
}

